﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class RemitterMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!IsPostBack)
            {
                //bntbrowse.Enabled = false;
                //bntgetdetails.Enabled = false;
                //rbOption.Focus();
                BindGrid();
            }
        }
        public void BindGrid()
        {
            RemitterEntity remitEntity = new RemitterEntity();
            List<Remitter> remitterLst = null;
            try
            {
                remitterLst = remitEntity.GetRemitterDetails("", ((User)Session["CurrentUserVO"]).CustomerID);

                if (remitterLst == null)
                {
                    gvCustomer.DataSource = null;
                    gvCustomer.DataBind();
                }
                else
                {
                    gvCustomer.DataSource = remitterLst;
                    gvCustomer.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<Remitter> lstCustomer = null;
            RemitterEntity remitEntity = new RemitterEntity();
            Remitter remit = new Remitter();
			string resp = string.Empty;
            lstCustomer = remitEntity.GetRemitterDetailsByRemitterId(txtRemitterId.Text.Trim(), ((User)Session["CurrentUserVO"]).CustomerID);

            if (lstCustomer != null)
                {
                    remit = new Remitter
                    {
                        RemitterID = txtRemitterId.Text.Trim(),
                        RemitterName = txtRemitterName.Text.Trim(),
                        CustomerID = ((User)Session["CurrentUserVO"]).CustomerID,
                        Address1 = txtAddress1.Text.Trim(),
                        Address2 = txtAddress2.Text.Trim(),
                        Address3 = txtAddress3.Text.Trim(),
                        City = txtCity.Text.Trim(),
                        State = txtState.Text.Trim(),
                        Country = txtCountry.Text.Trim(),
                        ZipCode = txtZipCode.Text.Trim(),
                        TelephoneNumber = txtPhone.Text.Trim(),
                        EmailAddress = txtEmail.Text.Trim(),
                        IsEnabled = chkIsenabled.Checked,
                        DiscountCode = txtDiscountcode.Value,
                        PenaltyCode = txtPenaltycode.Value,
                        //IsActive = chkActive.Checked
                    };
                    int retval = remitEntity.ModifyRemitter(remit, ((User)Session["CurrentUserVO"]).UserID);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Remitter modified successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error Modifying Remitter!";
                        btnClickPopup.Show();
                    }
                    
                }
                else
                {
                   remit = new Remitter
                    {
                        RemitterID = txtRemitterId.Text.Trim(),
                        RemitterName = txtRemitterName.Text.Trim(),
                        CustomerID = ((User)Session["CurrentUserVO"]).CustomerID,
                        Address1 = txtAddress1.Text.Trim(),
                        Address2 = txtAddress2.Text.Trim(),
                        Address3 = txtAddress3.Text.Trim(),
                        City = txtCity.Text.Trim(),
                        State = txtState.Text.Trim(),
                        Country = txtCountry.Text.Trim(),
                        ZipCode = txtZipCode.Text.Trim(),
                        TelephoneNumber = txtPhone.Text.Trim(),
                        EmailAddress = txtEmail.Text.Trim(),
                        IsEnabled = chkIsenabled.Checked,
                        DiscountCode = txtDiscountcode.Value,
                        PenaltyCode = txtPenaltycode.Value,
                        //IsActive = chkActive.Checked
                    };
                    int retval = remitEntity.Add(remit, ((User)Session["CurrentUserVO"]).UserID);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Remitter added successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error adding Remitter!";
                        btnClickPopup.Show();
                    }
                }
            BindGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            txtRemitterId.Text = "";
            txtRemitterName.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtAddress3.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            chkIsenabled.Checked = true;
            txtPenaltycode.Value = "";
            txtDiscountcode.Value = "";
            btnSubmit.Text = "Add";
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        //protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (rbOption.SelectedValue.Trim() == "1")
        //    {
        //        bntbrowse.Enabled = false;
        //        bntgetdetails.Enabled = false;
        //    }
        //    else
        //    {
        //        bntbrowse.Enabled = true;
        //        bntgetdetails.Enabled = true;
        //    }
        //}

        protected void bntbrowseDiscount_Click(object sender, EventArgs e)
        {
            string path;
            path = "RemitterMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchDiscount.aspx?textbox=" + txtDiscountcode.Value + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

        protected void bntbrowsePenalty_Click(object sender, EventArgs e)
        {
            string path;
            path = "RemitterMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchPenalty.aspx?textbox=" + txtPenaltycode.Value + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int retval = 0;
            try
            {
                Label remId = ((Label)gvCustomer.Rows[e.RowIndex].Cells[1].FindControl("lblRemitterID"));
                RemitterEntity custEntity = new RemitterEntity();
                retval = custEntity.DeleteRemitter(Convert.ToString(remId.Text), ((User)Session["CurrentUserVO"]).CustomerID);
                if (retval != 0)
                {
                    lblMessage.Text = "Remitter deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting remitter detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindGrid();
        }

    }
}